package com.qfedu.zf.my.controller;
import com.qfedu.zf.common.entity.Rent;
import com.qfedu.zf.common.utils.JwtUtil;
import com.qfedu.zf.common.vo.R;
import com.qfedu.zf.my.entity.User;
import com.qfedu.zf.my.service.MyService;
import com.qfedu.zf.my.vo.RentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/my")
public class MyController {

    @Autowired
    MyService myService;

    //点击我的界面 前端通过钩子函数查询信息返回前端，前端保存到全局变量中
    @GetMapping("/selectById")
    public R select(HttpServletRequest request){
        String lctoken = request.getHeader("Lctoken");
        String id = JwtUtil.parseToken(lctoken, "id").toString();
        int i = Integer.parseInt(id);
        User user = myService.selectById(i);
        return R.ok(user);
    }

    //    上传头像
    @PostMapping("/loadPicture")
    public R loadPicture(HttpServletRequest request,MultipartFile file) {
        System.out.println(file);
        String lctoken = request.getHeader("Lctoken");
        String id = JwtUtil.parseToken(lctoken, "id").toString();
        int i = Integer.parseInt(id);

        String s = myService.setPicture(file,i);
        System.out.println(s);
        return R.ok(s);
    }
    //    只上传头像
    @PostMapping("/load")
    public R load(MultipartFile file) {
        System.out.println(file);
        String s = myService.setP(file);
        System.out.println(s);
        return R.ok(s);
    }
//    修改名字
    @PostMapping("/updateName")
    public R updateName(@RequestBody User user){
        System.out.println(user);
       return myService.updateName(user);
    }
//    发布房源
    @PostMapping("/publish")
    public R publish(@RequestBody Rent rent){
        RentVo rentVo = new RentVo();
        BeanUtils.copyProperties(rent,rentVo);
        List<String> rentalType = rent.getRentalType();
        String s = rentalType.toString();
        rentVo.setRentalType(s);
        List<String> houseStar = rent.getHouseStar();
        String s1 = houseStar.toString();
        rentVo.setHouseStar(s1);
        String houseArea = rent.getHouseArea();
        rentVo.setHouseArea(Integer.parseInt(houseArea));
        rentVo.setHouseRent(Integer.parseInt(rent.getHouseRent()));
        System.out.println(")("+rentVo);
        return myService.publish(rentVo);
    }
//查寻发布的房子
    @GetMapping ("/selectOne")
    public R selectPublish(HttpServletRequest request){
        String lctoken = request.getHeader("Lctoken");
        String id = JwtUtil.parseToken(lctoken, "id").toString();
        int i = Integer.parseInt(id);
        return  myService.selectPublish(i);
    }

//    shan  chu
    @GetMapping("/deleteOne")
    public R deleteOne(Integer id){
        return myService.deleteOne(id);
    }

}
